package br.unifor.ads.pjappfinanceiro_dkp.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import br.unifor.ads.pjappfinanceiro_dkp.manager.UsuarioManager;
import br.unifor.ads.pjappfinanceiro_dkp.utils.Context;

public class FrmPrincipal extends JFrame {

	private static final long serialVersionUID = -3865004104121877823L;
	private static JInternalFrame internalFrame;
	private JTextField textField;
	private UsuarioManager usuarioManager = new UsuarioManager();
	private JPasswordField passwordField;
	private static FrmPrincipal frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new FrmPrincipal();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws PropertyVetoException 
	 */
	public FrmPrincipal() throws PropertyVetoException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 720, 517);
		JPanel contentPane = new JPanel();
		
		internalFrame = new JInternalFrame("Login");
        internalFrame.setSelected(true);
        internalFrame.setBounds(161, 144, 304, 198);
        contentPane.add(internalFrame, BorderLayout.CENTER);
        internalFrame.getContentPane().setLayout(null);
        
        JLabel lblNewLabel = new JLabel("Login:");
        lblNewLabel.setBounds(59, 20, 97, 14);
        internalFrame.getContentPane().add(lblNewLabel);
        
        textField = new JTextField();
        textField.setBounds(98, 17, 180, 20);
        internalFrame.getContentPane().add(textField);
        textField.setColumns(10);
        
        JLabel lblSenha = new JLabel("Senha:");
        lblSenha.setBounds(54, 51, 84, 14);
        internalFrame.getContentPane().add(lblSenha);
        
        JButton btnNewButton_1 = new JButton("OK");
        btnNewButton_1.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0) {
        		 String retorno = usuarioManager.logar(textField.getText(),
                         String.valueOf(passwordField.getPassword()));
         if (usuarioManager.getUsuarioLogado()) {
                 internalFrame.dispose();
                 internalFrame = null;
                 Context.setContext(frame);
         } else {
                 JLabel lblErro = new JLabel(retorno);
                 lblErro.setForeground(Color.RED);
                 lblErro.setBounds(60, 144, 250, 14);
                
                 internalFrame.getContentPane().add(lblErro);
                 internalFrame.repaint();
         }
        	}
        });
        btnNewButton_1.setBounds(24, 99, 117, 31);
        internalFrame.getContentPane().add(btnNewButton_1);
        
        JButton btnCancelar = new JButton("Cancelar");
        btnCancelar.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		dispose();
        	}
        });
        btnCancelar.setBounds(161, 99, 117, 31);
        internalFrame.getContentPane().add(btnCancelar);
        
        passwordField = new JPasswordField();
        passwordField.setBounds(98, 48, 180, 20);
        internalFrame.getContentPane().add(passwordField);
		
		montaMenu(contentPane);
		
		internalFrame.setVisible(true);
	}

	public void montaMenu(JPanel contentPane) {
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btnNewButton = new JButton("Clientes");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FrmCadCliente frmCadCliente = new FrmCadCliente();
				setContentPane(frmCadCliente);
				frame.setVisible(true);
				
			}
		});
		btnNewButton.setBounds(0, 0, 112, 68);
		contentPane.add(btnNewButton);

		JButton btnFornecedores = new JButton("Fornecedores");
		btnFornecedores.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				FrmCadFornecedor frmCadFornecedor = new FrmCadFornecedor();
				setContentPane(frmCadFornecedor);
				frame.setVisible(true);
				
			}
		});
		btnFornecedores.setBounds(111, 0, 130, 68);
		contentPane.add(btnFornecedores);

		JButton btnCaixa = new JButton("Caixa");
		btnCaixa.setBounds(241, 0, 106, 68);
		contentPane.add(btnCaixa);

		JButton btnContasPagar = new JButton("Contas Pagar");
		btnContasPagar.setBounds(347, 0, 117, 68);
		contentPane.add(btnContasPagar);

		JButton btnContasReceber = new JButton("Contas Receber");
		btnContasReceber.setBounds(463, 0, 144, 68);
		contentPane.add(btnContasReceber);

		JButton btnUsuario = new JButton("Usuário");
		btnUsuario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				FrmCadUsuario frmCadUser = new FrmCadUsuario();
				setContentPane(frmCadUser);
				frame.setVisible(true);

				
				
			}
		});
		btnUsuario.setBounds(606, 0, 98, 68);
		contentPane.add(btnUsuario);
	}
}
